package servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Donate;
import dao.DonateDao;

public class WriteInsertDonateServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Donate donate = new Donate();
		donate.setDonate_name(req.getParameter("insertDonateName"));
		donate.setDonate_price(req.getParameter("insertDonatePrice"));
		donate.setDonate_need(req.getParameter("choice_project_title"));
		donate.setDonate_email(req.getParameter("insertDonateEmail"));
		donate.setDonate_telephone(req.getParameter("insertDonateTelephone"));
		donate.setDonate_address(req.getParameter("insertDonateAddress"));
		donate.setDonate_postalcode(req.getParameter("insertDonatePostalcode"));
		donate.setDonate_alumni(req.getParameter("insertDonateAlumni"));
		String insertDonateClassInformation = null;
		if ("yes".equals(donate.getDonate_alumni())) {
			insertDonateClassInformation = req
					.getParameter("insertDonateClassInformation");
		}
		donate.setDonate_classInformation(insertDonateClassInformation);
		donate.setRemark(req.getParameter("insertDonateRemark"));

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(System.currentTimeMillis());
		donate.setDonate_date(date);

		int num = new DonateDao().writeInsertInfo(donate);

		HttpSession session = req.getSession();
		String choice_id = (String) session.getAttribute("choice_id");

		if (num == 1) {
			session.setAttribute("find_insert_id", 10);
		} else {
			session.setAttribute("find_insert_id", 11);
		}

		resp.sendRedirect("houtai/guanli_1.jsp?id=" + choice_id);
	}

}
